package com.inno.life.controller.controller;

import com.google.common.base.Preconditions;
import com.inno.life.common.annotiation.Log;
import com.inno.life.common.context.LoginUserContext;
import com.inno.life.common.entity.CommonReturnType;
import com.inno.life.common.entity.PageResult;
import com.inno.life.controller.convert.MomentCommentDTOConverter;
import com.inno.life.controller.dto.MomentCommentDTO;
import com.inno.life.domain.entity.MomentCommentBO;
import com.inno.life.domain.service.MomentCommentDomainService;
import com.inno.life.domain.utils.SensitiveWordUtil;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author sean
 * 评论业务controller
 */
@RestController
@RequestMapping("/life/moment/comment")
public class MomentCommentController {
    @Resource
    private MomentCommentDomainService momentCommentDomainService;
    @Resource
    private SensitiveWordUtil sensitiveWordUtil;

    /**
     * 评论
     */
    @PostMapping("/add")
    @Log(operationType = "评论")
    public CommonReturnType<Void> add(@RequestBody MomentCommentDTO momentCommentDTO){
        Preconditions.checkNotNull(momentCommentDTO.getMomentId(),"动态id不能为空");
        Preconditions.checkArgument(!StringUtils.isBlank(momentCommentDTO.getContent()),"评论内容不能为空");
        Long userId = LoginUserContext.getUserId();
        sensitiveWordUtil.checkAndThrowFormatError(momentCommentDTO.getContent());


        momentCommentDTO.setUserId(userId);
        MomentCommentBO momentCommentBO = MomentCommentDTOConverter.INSTANCE.convertDTOToBO(momentCommentDTO);
        this.momentCommentDomainService.add(momentCommentBO);
        return CommonReturnType.success();
    }

    /**
     * 删除评论
     */
    @PostMapping("/del")
    public CommonReturnType<Void> del(@RequestBody MomentCommentDTO momentCommentDTO){
        Preconditions.checkNotNull(momentCommentDTO.getId(),"评论id不能为空");
        Long userId = LoginUserContext.getUserId();
        momentCommentDTO.setCurrentUserId(userId);
        MomentCommentBO momentCommentBO = MomentCommentDTOConverter.INSTANCE.convertDTOToBO(momentCommentDTO);
        this.momentCommentDomainService.del(momentCommentBO);
        return CommonReturnType.success();
    }
    /**
     * 评论列表
     */
    @PostMapping("/list")
    public CommonReturnType<PageResult<MomentCommentDTO>> list(@RequestBody MomentCommentDTO momentCommentDTO){
        Preconditions.checkNotNull(momentCommentDTO.getMomentId(),"动态id不能为空");
        Preconditions.checkNotNull(momentCommentDTO.getPage(),"分页参数不能为空");
        Preconditions.checkNotNull(momentCommentDTO.getPageSize(),"分页参数不能为空");
        MomentCommentBO momentCommentBO = MomentCommentDTOConverter.INSTANCE.convertDTOToBO(momentCommentDTO);
        PageResult<MomentCommentBO> boResult = this.momentCommentDomainService.list(momentCommentBO);
        List<MomentCommentDTO> dtoList = MomentCommentDTOConverter.INSTANCE.convertBOListToDTOList(boResult.getList());
        return CommonReturnType.success(PageResult.of(dtoList,boResult.getTotal()));
    }
}
